package com.xsjiong.hoofen.entities;

import android.support.v4.util.SparseArrayCompat;
import com.xsjiong.hoofen.IO;
import com.xsjiong.hoofen.Logs;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class School implements DataEntity {
	private static final SparseArrayCompat<School> INSTANCES = new SparseArrayCompat<>();

	public static final School DEFAULT = School.getInstance("四川省成都市七中育才学校汇源校区", 280011);

	static void LoadStaticData(DataInputStream in) throws IOException {
		synchronized (INSTANCES) {
			for (int size = in.readInt(), tmp; size != 0; size--) {
				tmp = in.readInt();
				INSTANCES.put(tmp, School.getInstance(in.readUTF(), tmp));
			}
		}
	}

	static void SaveStaticData(DataOutputStream out) throws IOException {
		synchronized (INSTANCES) {
			out.writeInt(INSTANCES.size());
			School tmp;
			for (int i = 0; i < INSTANCES.size(); i++) {
				tmp = INSTANCES.valueAt(i);
				out.writeInt(tmp.id);
				out.writeBytes(tmp.name);
			}
		}
	}

	private String name;
	private int id;

	public static School getInstance(int id) {
		return getInstance(null, id);
	}

	public static School getInstance(String name, int id) {
		School ret = null;
		synchronized (INSTANCES) {
			ret = INSTANCES.get(id);
			if (ret == null) {
				ret = new School(name, id);
				INSTANCES.append(id, ret);
			} else if (name != null) ret.setName(name);
		}
		return ret;
	}

	private School() {
	}

	private School(String name, int id) {
		this.name = (name == null ? Integer.toString(id) : name);
		this.id = id;
	}

	public void setName(String name) {
		if (name != null)
			this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public int getID() {
		return this.id;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof School)) return false;
		return ((School) obj).id == id;
	}

	@Override
	public int hashCode() {
		return id;
	}

	@Override
	public void writeTo(DataOutputStream out) throws IOException {
		out.writeBytes(name);
		out.writeInt(id);
	}

	@Override
	public void readFrom(DataInputStream in) throws IOException {
		name = in.readUTF();
		id = in.readInt();
	}

	@Override
	public JSONObject toJSON() throws JSONException {
		JSONObject ret = new JSONObject();
		ret.put("name", name);
		ret.put("id", id);
		return ret;
	}

	@Override
	public void readFrom(JSONObject obj) {
		name = obj.optString("name");
		id = obj.optInt("id");
	}

	public static School loadFrom(DataInputStream in) throws IOException {
		School ret = new School();
		ret.readFrom(in);
		return ret;
	}

	public static School loadFrom(JSONObject obj) {
		School ret = new School();
		ret.readFrom(obj);
		return ret;
	}

	@Override
	public String toString() {
		if (name == null) return Integer.toString(id);
		return "[" + name + "]" + id;
	}

	public Exam[] getAllExams() {
		try {
			JSONObject obj = new JSONObject(IO.URLString("http://www.doofen.com/doofen/" + getID() + "/examList/exams"));
			if (!obj.optBoolean("success", false)) return null;
			JSONArray arr = obj.optJSONArray("data");
			Exam[] ret = new Exam[arr.length()];
			for (int i = 0; i < arr.length(); i++) {
				ret[i] = Exam.loadFrom(arr.optJSONObject(i));
				ret[i].setSchool(this);
			}
			return ret;
		} catch (Throwable t) {
			Logs.e("Doofen - getExams", t);
			return null;
		}
	}
}